﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utils.Lisence
{
    public static class AesHelper
    {
        //16位
        private const string AesIv = "{8E7F0284-5AAF-4D6D-88E2-BE677E37D753}";

        /// <summary>
        /// AES加密算法
        /// </summary>
        /// <param name="input">明文字符串</param>
        /// <param name="key">密钥（32位）</param>
        /// <returns>字符串</returns>
        public static string EncryptByAes(string input, string key)
        {
            var keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 31));
            using (var aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = keyBytes;
                aesAlg.IV = Encoding.UTF8.GetBytes(AesIv.Substring(0, 16));

                var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (var msEncrypt = new MemoryStream())
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(input);
                        }
                        var bytes = msEncrypt.ToArray();
                        return ByteToHexStr(bytes);
                    }
                }
            }
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="input">密文字节数组</param>
        /// <param name="key">密钥（32位）</param>
        /// <returns>返回解密后的字符串</returns>
        public static string DecryptByAes(string input, string key)
        {
            var inputBytes = StrToToHexByte(input);
            var keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 31));
            using (var aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = keyBytes;
                aesAlg.IV = Encoding.UTF8.GetBytes(AesIv.Substring(0, 16));

                var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (var msEncrypt = new MemoryStream(inputBytes))
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (var srEncrypt = new StreamReader(csEncrypt))
                        {
                            return srEncrypt.ReadToEnd();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] StrToToHexByte(string hexString)
        {
            if (hexString.Length % 2 != 0)
                hexString += " ";
            var returnBytes = new byte[hexString.Length / 2];
            for (var i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }

        /// <summary>
        /// 字节数组转16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ByteToHexStr(byte[] bytes)
        {
            var result = BitConverter.ToString(bytes);
            return result.Replace("-", "");
        }
    }
}
